package com.bestom.smartwatch.manage;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.bestom.smartwatch.helper.SQLiteHelper;
import com.bestom.smartwatch.util.LogUtil;

import java.util.ArrayList;

public class SQLiteManage {

    private SQLiteDatabase db;
    /** table name---> students                表名
     * _id INTEGER PRIMARY KEY AUTOINCREMENT,  表的主键id
     * device_id TEXT,                         手表设备 id
     * students_name TEXT,                     学生姓名
     * students_id TEXT,                       学生学号
     * students_class TEXT                     学生班级
     **/
    private String table = "students";

    public SQLiteManage(Context context){
        db = new SQLiteHelper(context).getWritableDatabase();
    }
    /**学生注册添加*/
    public int add(String deviceId, String studentName, String studentId, String studentClass){
        if (nameExist(studentName)) return -1;
        ContentValues values = new ContentValues();
        values.put("device_id", deviceId);
        values.put("students_name", studentName);
        values.put("students_id", studentId);
        values.put("students_class", studentClass);
        long add = db.insert(table,null,values);
        LogUtil.log("add",String.valueOf(add));
        return 1;
    }
    /**通过设备id查询学生属性信息*/
    public Cursor select(String deviceId){
        return db.rawQuery("select * from "+ table + " where device_id=?",new String[] {deviceId});
    }

    public String selectIdByName(String studentName){

        String deviceId = "";
        Cursor cursor = db.rawQuery("select device_id from " + table + " where students_name=?", new String[]{studentName});
        if (cursor.getCount() > 0){
            while (cursor.moveToNext()){
                deviceId = cursor.getString(cursor.getColumnIndex("device_id"));
            }
        }
        return deviceId;
    }

    /**查询学生属性表所有内容*/
    public Cursor selectAll(){
        return db.rawQuery("select * from "+ table,null);
    }

    /**判断该设备id是否存在学生属性表中*/
    public boolean exist(String deviceId){
        Cursor cursor = db.rawQuery("select * from "+ table + " where device_id=?",new String[] {deviceId});
        if (cursor.getCount() != 0) return true;
        return false;
    }
    /**判断该姓名是否存在学生属性表中*/
    public boolean nameExist(String studentName){
        Cursor cursor = db.rawQuery("select * from "+ table + " where students_name=?",new String[] {studentName});
        if (cursor.getCount() != 0) return true;
        return false;
    }
    /**更改学生属性信息*/
    public int update(String deviceId, String popStudentName, String popStudentId, String popStudentClass) {

        if (nameExist(popStudentName)) return -1;
        ContentValues values = new ContentValues();
        values.put("students_name", popStudentName);
        values.put("students_id", popStudentId);
        values.put("students_class", popStudentClass);
        int update = db.update(table, values, "device_id = ?", new String[]{deviceId});
        LogUtil.log("update",String.valueOf(update));
        return 0;
    }

    /**table name---> students_data             表名
     * _id INTEGER PRIMARY KEY AUTOINCREMENT,   表的主键id
     * device_id TEXT,                          手表设备 id
     * students_name TEXT,                      学生姓名
     * students_max_temp TEXT,                  最高温度
     * students_body_temp TEXT,                 身体温度
     * students_skin_temp TEXT,                 体表温度
     * students_step TEXT,                      步数
     * students_calories TEXT,                  卡路里
     * students_distance TEXsT,                 公里数
     * students_heart_rate TEXT,                心率
     * students_systolic_blood_pressure TEXT,   收缩血压
     * students_diastolic_blood_pressure TEXT,  舒张血压
     * students_blood_oxygen TEXT,              血氧
     * students_time TEXT                       记录时间
     **/
    private String dataTable = "students_data";

    /**将测量到数据添加到学生数据表*/
    public void addData(String deviceId, String studentName, String maxTemp, String bodyTemp, String skinTemp,
                        String step, String calories, String distance, String heartRate,
                        String systolicBloodPressure, String diastolicBloodPressure, String bloodOxygen, String time){
        ContentValues values = new ContentValues();
        values.put("device_id", deviceId);
        values.put("students_name", studentName);
        values.put("students_max_temp", maxTemp);
        values.put("students_body_temp", bodyTemp);
        values.put("students_skin_temp", skinTemp);
        values.put("students_step", step);
        values.put("students_calories", calories);
        values.put("students_distance", distance);
        values.put("students_heart_rate", heartRate);
        values.put("students_systolic_blood_pressure", systolicBloodPressure);
        values.put("students_diastolic_blood_pressure", diastolicBloodPressure);
        values.put("students_blood_oxygen", bloodOxygen);
        values.put("students_time", time);
        long add = db.insert(dataTable,null,values);
        LogUtil.log("add",String.valueOf(add));
    }
    /**查询学生数据表所有内容*/
    public Cursor selectAllData(){
        return db.rawQuery("select * from "+ dataTable,null);
    }
    /**查询学生数据表当天的信息
     * time 格式：2020-11-10
     * countRow 一共查询多少行数
     * fromRow  从第几行开始查询
     * */
    public Cursor selectNowAllData(String time, String countRow, String fromRow){
        if (countRow == null || fromRow == null) return db.rawQuery("select * from "+ dataTable + " where students_time like ?",new String[]{time + "%"});
        return db.rawQuery("select * from "+ dataTable + " where students_time like ?" +  " limit ? offset ?",new String[]{time + "%", countRow, fromRow});
    }
    /**查询学生数据表最后几行数据
     * number 一共查询多少行数
     * */
    public Cursor selectLastNumberData(int number){
        return db.rawQuery("select * from "+ dataTable + " order by _id desc limit ?", new String[]{String.valueOf(number)});
    }
    /**根据姓名查询某学生所有数据信息*/
    public Cursor selectStudentAllData(String studentName){
        return db.rawQuery("select * from "+ dataTable + " where device_id = ?",new String[]{selectIdByName(studentName)});
    }

    /**根据姓名、时间查询学生所有数据信息*/
    public Cursor selectStudentAllDataByNameAndTime(String studentName, String time){
        if (studentName == null && time == null) return selectAllData();
        else {
            if (studentName == null) return selectNowAllData(time,null,null);
            else if (time == null) return selectStudentAllData(studentName);
            else return db.rawQuery("select * from "+ dataTable + " where students_name = ? and students_time like ?",new String[]{studentName, time + "%"});
        }
    }
}
